// pages/forgetPassWord2/forgetPassWord2.js
import ForgetPassWordApi from '../../api/forgetPassWord.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    passWord:'',
    passWordNew:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      phone:options.phone
    })
  },
  /**
   * 返回
   */
  handlerGobackClick(){
    wx.navigateBack()
  },
  /**
   * 重置密码
   */
  async resetPassWord(){
    //验证
    const passWord = this.data.passWord
    const passWordNew = this.data.passWordNew
    if(passWord == ''){
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
    }
    if(passWord !== passWordNew){
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(passWord.length < 6){
      wx.showToast({
        title: '密码不能小于6个字符',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(passWord.length > 12){
      wx.showToast({
        title: '密码不能大于12个字符',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const param = {
      telNum:this.data.phone,
      newPassword: this.data.passWord
    }
    const res = await ForgetPassWordApi.resetPasswordByVerifyCode(param)
    if(res.code == 100){
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000
      })
    }else {
      //提交成功跳转到登录页面
      wx.showToast({
        title: '成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(()=>{
        wx.navigateTo({
          url: '/pages/login/login'
        })
      },2000)
    }

  },
  /**
   * input 数据绑定
   */
  updateValue(e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail
    this.setData(nameMap)
  }
})
